const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    setting: app.globalData.setting,
    userInfo: app.globalData.userInfo,
    capsule: app.globalData.capsule,
    boxes: [],
    records: [],
    showmodal: false,
    treasureAmount: 0,
    bg: ["#f8c500", "#e48cb6", "#e5accd", "#dd0600", "#e76867", "#fdd400", "#d2dc87", "#25953f", "#c4e1d1", "#66c0b9"],
    move: ['box-rotate 1s ease 0.65s', 'box-scale 1s', 'box-jump 500ms ease 0.75s', 'box-rotate 1s', 'box-scale 1s ease 0.45s', 'box-jump 700ms', 'box-rotate 800ms', 'scale 1s', 'box-jump 500ms'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      setting: app.globalData.setting,
      userInfo: app.globalData.userInfo,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfo()

    this.getBoxes()
    this.getRecords()
  },

  getUserInfo() {
    wx.cloud.callFunction({
      name: 'user',
    }).then(res => {
      console.log('userInfo', res.result)
      this.setData({
        userInfo: res.result
      })
    })
  },

  getBoxes() {
    wx.cloud.callFunction({
      name: 'box',
      data: {
        op: 'getBox'
      }
    }).then(res => {
      console.log('boxes', res.result)
      this.setData({
        boxes: this.randomArray(res.result),
        bg: this.randomArray(this.data.bg),
        move: this.randomArray(this.data.move),
      })
    })

  },

  /**
   * 打开宝箱
   */
  openBox(e) {
    let that = this
    let awardOpenBoxTimes = this.data.setting.awardOpenBoxTimes
    let index = e.currentTarget.dataset.index
    let box = this.data.boxes[index]
    this.setData({
      box: box
    })
    let treasureAmount = 0
    for (let i = 0; i < box.treasures.length; i++) {
      treasureAmount += box.treasures[i].amount
    }


    wx.cloud.callFunction({
      name: 'box',
      data: {
        op: 'openBox',
        _id: box._id,
        treasures: box.treasures,
      }
    }).then(res => {
      console.log('openBox', res.result)
      if (res.result == 'notEnough') {
        wx.showModal({
          // content: '观看视频可获得 ' + awardOpenBoxTimes + ' 次打开礼盒的机会',
          // confirmText: '观看视频',
          content: '花300金币，可获得 ' + awardOpenBoxTimes + ' 次打开盲盒的机会。',
          confirmText: '好的',
          cancelText: '明日再来',
          success(res) {
            if (res.confirm) {
              that.addTimes()
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (res.result == 'openedByOthers') {
        this.getBoxes()
        wx.showToast({
          title: '手慢了，被别人打开了',
          icon: 'none'
        })
      } else if (res.result == 'saved') {

        this.setData({
          showmodal: 'openbox',
          treasureAmount: treasureAmount
        });
        this.getUserInfo()
        this.getBoxes()
      }

    })

  },

  addTimes() {
    wx.cloud.callFunction({
      name: 'box',
      data: {
        op: 'updateOpenTimes',
      }
    }).then(res => {
      console.log(res)
      this.getUserInfo()
    })
  },
  /**
   * 获取消息信息
   */
  getRecords() {

    wx.cloud.callFunction({
      name: 'getRecord',
      data: {
        op: 'openBoxRecord'
      }
    }).then(res => {
      console.log('records', res.result)
      this.setData({
        records: res.result
      })
    })
  },
  randomArray(arr) {
    var newArr = [];
    var len = arr.length;
    for (var i = 0; i < len; i++) {
      var index = Math.floor(Math.random() * arr.length); //随机下标
      newArr.push(arr[index]); //将随机出的元素，存放新数组newArr中去
      arr.splice(index, 1); //    将随机出的元素在arr中删除            
    }
    var res = [...newArr, ...arr];
    return res
  },
  /**
   * 点击modal空白区域
   */
  hideModal: function () {
    this.setData({
      showmodal: false
    });
  },
  navTo() {
    wx.navigateTo({
      url: '/pages/hideBox/index',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})